package com.yanqu.road.server.manger.match;

import com.yanqu.road.entity.config.match.MatchInfo;
import com.yanqu.road.entity.config.match.MatchReward;
import com.yanqu.road.logic.bussiness.match.MatchBussiness;
import com.yanqu.road.server.TempMgr;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：zfr
 * @date ：2021/2/4 9:46
 */
public class MatchMgr extends TempMgr {
    /**
     * 杂艺基本配置 level-list
     */
    public static Map<Integer, List<MatchInfo>> matchListMap;
    /**
     * 杂艺奖励配置 type-list
     */
    public static Map<Integer, List<MatchReward>> matchRewardsMap;
    /**
     * 奖励留存天数
     */
    public static final int rewardSaveDays = 30;

    @Override
    public boolean reloadConfig() throws Exception {
        matchListMap = MatchBussiness.getMatchListMap();
        matchRewardsMap = MatchBussiness.getMatchRewardsMap();
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean init() throws Exception {
        matchListMap = new ConcurrentHashMap<>();
        matchRewardsMap = new ConcurrentHashMap<>();
//        addServerGroupReloadListener();
        return reload();
    }

    public static MatchInfo getMatchInfo(int level, int matchId) {
        List<MatchInfo> matchInfos = matchListMap.get(level);
        if (matchInfos != null) {
            for (MatchInfo matchInfo : matchInfos) {
                if (matchInfo.getId() == matchId) {
                    return matchInfo;
                }
            }
        }
        return null;
    }

    /**
     * 根据排行取奖励
     * @param level
     * @param matchId
     * @param index
     * @return
     */
    public static MatchReward getReward(int level, int matchId, int index) {
        int rewardsType = 0;
        MatchInfo matchInfo = getMatchInfo(level, matchId);
        if (matchInfo != null) {
            rewardsType = matchInfo.getRewardsType();
        }
        List<MatchReward> matchRewards = matchRewardsMap.get(rewardsType);
        if (matchRewards != null) {
            for (MatchReward matchReward : matchRewards) {
                if ((index >= matchReward.getParam().get(0) || matchReward.getParam().get(0) == -1) && (index <= matchReward.getParam().get(1) || matchReward.getParam().get(1) == -1)) {
                    return matchReward;
                }
            }
        }
        return null;
    }

    //是否是系统开放时间
    public static boolean isMatchSystemOpenTime() {
        return true;
//        MatchServerGroup serverGroup = MatchServerGroupMgr.getServerGroupByServerId(GameServer.getInstance().getServerId());
//        return serverGroup != null && serverGroup.getMatchState() == 1;
    }

//    private void addServerGroupReloadListener(){
//        MatchServerGroupMgr.addReloadListener(new ManagerReloadListener() {
//            @Override
//            public void notifyReload() {
//                List<GamePlayer> playerList = GamePlayerMgr.getCopyPlayerList();
//                for (GamePlayer onlinePlay : playerList) {
//                    if (onlinePlay.getState() == PlayerState.ONLINE) {
//                        if (isMatchSystemOpenTime()) {
//                            onlinePlay.getModule(MatchModule.class).crossServerGroupReload();
//                        } else {
//                            //同步区服分组
//                            VarietyProto.SyncGroupServerReqMsg.Builder groupServerListSyncMsg = VarietyProto.SyncGroupServerReqMsg.newBuilder();
//                            onlinePlay.sendChannelPacket(CrossProtocol.C_VARIETY_SYNC_GROUP_SERVER_LIST, groupServerListSyncMsg);
//                        }
//                    }
//                }
//            }
//        });
//    }

    @Override
    public boolean stop() {
        return false;
    }
}
